package com.ruoyi.behavior.controller;

import com.ruoyi.behavior.domain.Replies;
import com.ruoyi.behavior.domain.dto.*;
import com.ruoyi.behavior.domain.dto.filter.FilterDto;
import com.ruoyi.behavior.domain.dto.filter.SkuFilterDto;
import com.ruoyi.behavior.domain.dto.filter.SupFilterDto;
import com.ruoyi.behavior.domain.vo.VerReviewVo;
import com.ruoyi.behavior.service.IVerReviewService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户行为Controller
 *
 * @author ruoyi
 * @date 2025-09-10
 */
@RestController
@RequestMapping("/behavior/review")
@Tag(name = "用户行为接口", description = "用户行为操作")
public class VerReviewController extends BaseController {
    @Autowired
    private IVerReviewService verReviewService;

    /**
     * 根据categoryId获取评论
     */
    @Operation(summary = "根据categoryId获取评论", description = "传入categoryId，返回评论列表")
    @RequestMapping("/getReviewByCategoryId")
    public AjaxResult getReviewByCategoryId(@RequestParam Long categoryId) {
        List<CategoryReviewDto> list = verReviewService.getReviewByCategoryId(categoryId);
        return success(list);
    }

    /**
     * 分页
     */
    @Operation(summary = "分页", description = "传入分页参数，返回分页数据")
    @RequestMapping("/list")
    public TableDataInfo list(CategoryReviewDto categoryReviewDto) {
        // 设置默认分页参数：第1页，每页20条
        startPage(1, 20);
        List<CategoryReviewDto> list = verReviewService.pageList(categoryReviewDto);
        return getDataTable(list);
    }

    /**
     * 根据skuId获取sku信息
     */
    @Operation(summary = "根据skuId获取sku信息", description = "传入skuId，返回sku信息")
    @GetMapping("/getSkuInfo/{skuId}")
    public AjaxResult getSkuInfo(@PathVariable Long skuId) {
        SkuReviewDto skuDto = verReviewService.getSkuInfo(skuId);
        return success(skuDto);
    }

    /**
     * 添加评论
     */
    @Operation(summary = "添加评论", description = "传入评论信息，返回添加结果")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VerReviewVo verReviewVo) {
        return success(verReviewService.add(verReviewVo));
    }

    /**
     * 根据sku_id获取统计信息与评论列表
     */
    @Operation(summary = "根据sku_id获取统计信息与评论列表", description = "传入sku_id，返回统计信息与评论列表")
    @PostMapping("/getSkuReviewList/{skuId}")
    public AjaxResult getSkuReviewList(@PathVariable Long skuId, @RequestBody SkuFilterDto filter) {
        SkuIdRequestDto request = new SkuIdRequestDto();
        request.setSkuId(skuId);
        request.setFilter(filter);
        SkuReviewListDto skuReviewListDto = verReviewService.getSkuReviewList(request);
        return success(skuReviewListDto);
    }

    /**
     * 根据商品的spu_id获取评论列表
     *
     * @param spuId
     * @return
     */
    @Operation(summary = "根据商品的spu_id获取评论列表", description = "根据商品的spu_id获取评论列表")
    @PostMapping("/getSpuReviewList/{spuId}")
    public AjaxResult getSpuReviewList(@PathVariable Long spuId, @RequestBody(required = false) SupFilterDto filter) {
        SupIdRequestDto request = new SupIdRequestDto();
        request.setSpuId(spuId);
        request.setFilter(filter);

        List<SpuReviewDto> spuReviewDto = verReviewService.getSpuReviewList(request);
        return success(spuReviewDto);
    }

    /**
     * 根据用户id获取订单项和评论
     */
    @Operation(summary = "根据用户id获取订单项和评论", description = "传入用户id和筛选条件，返回订单项和评论")
    @PostMapping("/getOrderItemAndReview/{userId}")
    public AjaxResult getOrderItemAndReview(@PathVariable Long userId,
            @RequestBody(required = false) FilterDto filter) {
        // 创建请求对象
        GetOrderItemAndReviewRequestDto request = new GetOrderItemAndReviewRequestDto();
        request.setUserId(userId);
        request.setFilter(filter);

        List<ReviewResultDto> list = verReviewService.getOrderItemAndReview(request);
        return success(list);
    }

    /**
     * 修改评论
     */
    @Operation(summary = "修改评论", description = "传入评论信息，返回修改结果")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody VerReviewVo verReviewVo) {
        return success(verReviewService.edit(verReviewVo));
    }

    /**
     * 回复评论
     */
    @Operation(summary = "回复评论", description = "传入评论信息，返回修改结果")
    @PostMapping("/reply")
    public AjaxResult reply(@RequestBody Replies replies) {
        return success(verReviewService.reply(replies));
    }

}
